#include <iostream>
#include <vector>
using namespace std;

struct TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

void printPaths(TreeNode* root, vector<int>& path) {
  if (!root) {
    return;
  }

  path.push_back(root->val);

  if (!root->left && !root->right) { 
    for (int i = 0; i < path.size(); i++) {
      cout << path[i] << " ";
    }
    cout << endl;
  } else {
    printPaths(root->left, path);
    printPaths(root->right, path);
  }

  path.pop_back(); 
}

int main() {
  TreeNode* root = new TreeNode(1);
  root->left = new TreeNode(2);
  root->right = new TreeNode(3);
  root->left->left = new TreeNode(4);
  root->left->right = new TreeNode(5);

  vector<int> path;
  printPaths(root, path);

  return 0;
}